<?php


namespace App\Repositories;


use App\Exceptions\BasicException;

class VersionManagementRepository
{
    /**
     * @param array $params
     * @return mixed
     */
    public function getList(array $params)
    {
        $whereArr = [
            'app_type'                 => $params['app_type'],
        ];

        if (isset($params['android_application_type'])) {
            $whereArr['android_application_type'] = $params['android_application_type'];
        }

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_version_management')->where($whereArr)->where($whereFunc)->orderBy('id', 'desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

    /**
     * @param $param array 插入数据
     * @return mixed
     * @throws BasicException
     * @author zlh
     */
    public function addManagement($param)
    {
        $addConfig = app('model_version_management')->create($param);
        $this->delReKey();

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 修改app版本管理信息
     * @param $where array 修改条件
     * @param $param array 修改数据
     * @return mixed
     * @throws BasicException
     */
    public function saveManagement($where, $param)
    {
        $saveConfig = app('model_version_management')->where($where)->update($param);
        $this->delReKey();

        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * @param $where array 修改条件
     * @return mixed
     * @throws BasicException
     */
    public function getManagementInfo($where)
    {
        $info = app('model_version_management')->where($where)->first();

        if ($info) return $info;
        throw new BasicException(93047);
    }

    /**
     * @param $id int 删除主键ID
     * @return mixed
     * @throws BasicException
     */
    public function delManagement($id)
    {
        $res = app('model_version_management')->where(["id" => $id])->delete();
        $this->delReKey();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 获取app设置信息
     * @param $params
     * @return mixed
     */
    public function getAppSettingList($params)
    {
        $whereArr = ['app_type' => $params['app_type']];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_app_setting')->where($whereArr)->where($whereFunc)->orderBy('id', 'desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

    /**
     * 新增app设置信息
     * @param $param array 新增参数
     * @return mixed
     * @throws BasicException
     */
    public function addAppSetting($param)
    {
        $addConfig = app('model_app_setting')->create($param);
        $this->delReKey();

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 修改app设置信息
     * @param $where array 修改条件
     * @param $param array 修改数据
     * @return mixed
     * @throws BasicException
     */
    public function saveAppSetting($where, $param)
    {
        $saveConfig = app('model_app_setting')->where($where)->update($param);
        $this->delReKey();

        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * 获取app设置信息
     * @param $params
     * @return mixed
     */
    public function getVersionSettingList($params)
    {
        $whereArr = ['app_type' => $params['app_type']];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_version_setting')->where($whereArr)->where($whereFunc)->orderBy('id', 'desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

    /**
     * 新增app版本设置信息
     * @param $param array 新增参数
     * @return mixed
     * @throws BasicException
     */
    public function addAppVersionSetting($param)
    {
        $addConfig = app('model_version_setting')->create($param);
        $this->delReKey();

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 修改app版本设置信息
     * @param $where array 修改条件
     * @param $param array 修改数据
     * @return mixed
     * @throws BasicException
     */
    public function saveAppVersionSetting($where, $param)
    {
        $saveConfig = app('model_version_setting')->where($where)->update($param);
        $this->delReKey();

        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * @param $id int 删除主键ID
     * @return mixed
     * @throws BasicException
     */
    public function delAppVersionSetting($id)
    {
        $res = app('model_version_setting')->where(["id" => $id])->delete();
        $this->delReKey();

        if ($res) return $res;
        throw new BasicException(93044);
    }


    public function delReKey()
    {
        $appKey = 'act:version:app_setting:2';
        app('redis')->del($appKey);
    }
}